package com.yx.shop.web.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yx.shop.common.Const;
import com.yx.shop.entitly.User;
import com.yx.shop.entitly.pojo.ResponseResult;
import com.yx.shop.service.admin.AdminUserService;
import com.yx.shop.service.admin.impl.AdminUserServiceImpl;
import com.yx.shop.web.controller.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Classname AdminUserController
 * @Description TODO:
 * @Date 2021/05/10 15:25
 * @Created by yx
 */
@WebServlet("/admin/user")
public class AdminUserController extends BaseServlet {
    private AdminUserService adminUserService = new AdminUserServiceImpl();

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/10 15:57
     * @Description:简单的验证登录
     **/
    public String login2(HttpServletRequest request, HttpServletResponse response) {
        //1.获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = adminUserService.login(username, password);
        if (user != null) {
            //保存数据到session当中
            //管理员登录状态--->String IS_ADMIN_LOGIN="admin";
            request.getSession().setAttribute(Const.UserInfo.IS_ADMIN_LOGIN, user);
            return Const.REDIRECT + "/admin/admin.jsp";
        } else {
            request.setAttribute("msg", "用户名或密码有误或不是管理员");
            return Const.FORWORD + "/admin/login.jsp";
        }
    }

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/10 15:57
     * @Description:较为严谨的登录验证
     **/
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //业务层
        ResponseResult<User> userResponseResult = adminUserService.loginForAdmin(username, password);
        if (userResponseResult.getCode() == 3) {
            //处理登录成功
            request.getSession().setAttribute(Const.UserInfo.IS_ADMIN_LOGIN, userResponseResult.getData());
            return Const.REDIRECT + "/admin/admin.jsp";
        }
        request.setAttribute("msg", userResponseResult);
        System.out.println(userResponseResult);
        return Const.FORWORD + "/admin/login.jsp";
    }

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/10 16:22
     * @Description:查询用户
     **/
    public String search(HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
        //1.获取参数
        String username = request.getParameter("username");
        String gender = request.getParameter("gender");

        //2.访问业务层获取数据
        List<User> userList = adminUserService.searchByKey(username, gender);
        //转为json
        ObjectMapper om = new ObjectMapper();
        String string = om.writeValueAsString(userList);
        return string;
    }

    /**
     * @Author:yx
     * @return java.lang.String
     * @Date 2021/05/10 16:40
     * @param request
     * @param response
     * @Description:根据uid删除用户
     **/
    public String deleteUser(HttpServletRequest request, HttpServletResponse response) {
        //判断是否登录
        User user = (User) request.getSession().getAttribute(Const.UserInfo.IS_ADMIN_LOGIN);
        if (user == null) {
            //重定向至管理员登录界面
            return Const.REDIRECT + "/admin/login.jsp";
        }
        //1.获取参数
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        boolean flag = adminUserService.deleteUserByUid(uid);
        System.out.println("deleteUser=============================>"+flag);
        //删除成功,重新查询---->前端页面使用ajax,需要返回参数,故不重定向
//        return Const.REDIRECT + "/admin/user/search";
        return flag+"";
    }
}
